tg-me.com/pythonetc/712
Last Update:
The reveal_type
function doesn't exist. However, if you call it and then run a type-checker (like mypy or pyright) on the file, it will show the type of the passed object:
a = 1Now, let's run mypy:
reveal_type(a)
reveal_type(len)
$ mypy tmp.pyIt's quite helpful to see what type mypy inferred for the variable in some tricky cases.
tmp.py:2: note: Revealed type is "builtins.int"
tmp.py:3: note: Revealed type is "def (typing.Sized) -> builtins.int"
For convenience, the
reveal_type
function was also added in typing
module in Python 3.11:from typing import reveal_typeAnd for curious, here is the definition:
a = 1
reveal_type(a)
# prints: Runtime type is 'int'
reveal_type(len)
# prints: Runtime type is 'builtin_function_or_method'
def reveal_type(__obj: T) -> T:
print(
f"Runtime type is {type(__obj).__name__!r}",
file=sys.stderr,
)
return __obj
BY Python etc
Warning: Undefined variable $i in /var/www/tg-me/post.php on line 280
Share with your friend now:
tg-me.com/pythonetc/712